home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / pc / Open Me for REALbasic 3 / REALbasic 3.2 / Goodies / REAL Software / REALbasic Plug-ins SDK / Examples / PluginTest / pluginTest.cpp next >
Encoding:
Text File  |  2001-01-24  |  7.6 KB  |  291 lines

  1. // pluginTest.cpp
  2. //
  3. //    This file makes a test plugin -- a plugin intended to demonstrate and test
  4. //    as many parts of the REALbasic plugin SDK as possible.  It is based on the
  5. //    boxControl plug-in, so has many initial similarities; but we'll keep
  6. //    boxControl simple while this one will grow more and more complex as we add
  7. //    more test cases.  Feel free to send me more test code for this plugin!
  8. //
  9. // Document Author: Joe Strout <joe@realsoftware.com>
  10. //
  11. // Revision History
  12. //    Nov 07 2000 -- JJS (1)    initial creation, adapted from boxControl.cpp
  13. //    Jan 24 2001 -- JJS (1)    added test of static/global class constructors/destructors
  14.  
  15. // include the RB plugin headers
  16. #include "rb_plugin.h"
  17.  
  18. // test of static/global class constructors/destructors
  19. static class testclass
  20. {
  21. public:
  22.     testclass();
  23.     ~testclass();
  24. } testclassobj;
  25.  
  26. testclass::testclass()
  27. {
  28.     // To test global constructors, uncomment this line:
  29.     //DebugStr("\pconstructing testclassobj");
  30. }
  31.  
  32. testclass::~testclass()
  33. {
  34.     // To test global destructors, uncomment this line:
  35.     //DebugStr("\pdestructing testclassobj");
  36. }
  37.  
  38.  
  39. // forward declaration of our custom controls and classes
  40. extern struct REALcontrol testControl;
  41.  
  42. REALevent testEvents[] = {
  43.     { "Action(Red As Integer)" }
  44. };
  45.  
  46. struct testData
  47. {
  48.     int fillColor;            // color in RB color format (xxRRGGBB)
  49. };
  50.  
  51. static void testInit(REALcontrolInstance)
  52. {
  53. }
  54.  
  55. #pragma mark testDraw
  56. #ifdef TARGET_WIN32
  57. static void testDraw(REALcontrolInstance instance, REALgraphics context)
  58. #else
  59. static void testDraw(REALcontrolInstance instance)
  60. #endif
  61. {
  62.     // Find the bounds rect
  63.     Rect r;
  64.     REALGetControlBounds(instance, &r);
  65.  
  66.     // Make sure our aspect ratio is constant.
  67.     short height = r.bottom-r.top, width = r.right-r.left;
  68.     if (width < height) {
  69.         // shorten height to match width
  70.         height = width;
  71.         r.bottom = r.top + height;
  72.         if (REALinRuntime()) REALSetControlPosition( instance, kREALHeight, height );
  73.     } else if (height < width) {
  74.         // shorten width to match height
  75.         width = height;
  76.         r.right = r.left + width;
  77.         if (REALinRuntime()) REALSetControlPosition( instance, kREALWidth, width );
  78.     }
  79.     
  80.     // Get our custom data into "data"
  81.     ControlData(testControl, instance, testData, data);
  82.  
  83.     // Do the drawing (using native graphics tooltest)
  84.   #if TARGET_WIN32
  85.     // Windows implementation:
  86.  
  87.     HDC hDC = REALGraphicsDC(REALGetControlGraphics(instance));
  88.  
  89.     HBRUSH fillBrush = CreateSolidBrush(RGB(
  90.         (data->fillColor >> 16), (data->fillColor >> 8) & 0xFF, (data->fillColor & 0xFF)));
  91.     HBRUSH lineBrush = CreateSolidBrush(RGB(0,0,0));
  92.     HPEN oldPen, linePen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
  93.  
  94.     RECT winr;
  95.     ::SetRect(&winr, r.left, r.top, r.right, r.bottom);
  96.     ::FillRect(hDC, &winr, fillBrush);
  97.  
  98.     ::FrameRect(hDC, &winr, lineBrush);
  99.  
  100.     oldPen = (HPEN) SelectObject(hDC, linePen);
  101.     ::MoveToEx(hDC, r.left, r.top, nil);
  102.     ::LineTo(hDC, r.right - 1, r.bottom - 1);
  103.  
  104.     SelectObject(hDC, oldPen);
  105.  
  106.     DeleteObject(fillBrush);
  107.     DeleteObject(lineBrush);
  108.     DeleteObject(linePen);
  109.  
  110.   #else
  111.     // Mac implementation:
  112.     RGBColor oldCol, fillCol, lineCol = {0,0,0};
  113.  
  114.     GetForeColor(&oldCol);
  115.     fillCol.red = (data->fillColor >> 16) * 257;
  116.     fillCol.green = ((data->fillColor >> 8) & 0xff) * 257;
  117.     fillCol.blue = ((data->fillColor & 0xff) * 257);
  118.     RGBForeColor(&fillCol);
  119.     PaintRect(&r);
  120.  
  121.     RGBForeColor(&lineCol);
  122.     FrameRect(&r);
  123.     
  124.     MoveTo(r.left, r.top);
  125.     LineTo(r.right - 1, r.bottom - 1);
  126.  
  127.     RGBForeColor(&oldCol);
  128.    #endif
  129. }
  130.  
  131. static void testMakeRed(REALcontrolInstance instance)
  132. {
  133.     // get our private data
  134.     ControlData(testControl, instance, testData, data);
  135.  
  136.     // make the test red
  137.     data->fillColor = 0xFF0000;
  138.  
  139.     // invoke the event handler, if there is one
  140.     void (*fp)(REALcontrolInstance instance, int);
  141.     fp = (void (*)(REALcontrolInstance instance, int)) REALGetEventInstance(instance, &testEvents[0]);
  142.     if (fp) fp(instance, data->fillColor >> 16);
  143.  
  144.     // just for testing, let's also try out some of the newer control methods
  145.     REALSetControlPosition( instance, kREALTop, REALGetControlPosition(instance, kREALTop) - 20 );
  146. }
  147.  
  148. static REALmemoryBlock TestNewMemoryBlock(REALcontrolInstance, int bytes)
  149. {
  150.  
  151.     // Test the REALNewMemoryBlock function by simply calling it and returning
  152.     // the result to the RB app, which can check its validity.
  153.     return REALNewMemoryBlock(bytes);
  154. }
  155.  
  156. static int TestMemoryBlock(REALcontrolInstance)
  157. {
  158.     // This is a general test function for all the MemoryBlock APIs.
  159.     // Returns 0 for no error, nonzero for errors.
  160.     REALmemoryBlock m = REALNewMemoryBlock(42);
  161.     if (not m) return 1;
  162.     if (REALMemoryBlockGetSize(m) != 42) return 2;
  163.     REALUnlockObject((REALobject)m);
  164.     
  165.     if (REALNewMemoryBlock(-5)) return 3;
  166.     if (REALNewMemoryBlock(0)) return 4;
  167.     
  168.     char buf[] = "01234567";
  169.     m = REALPtrToMemoryBlock(buf);
  170.     if (not m) return 5;
  171.     unsigned long *ptr = (unsigned long*)REALMemoryBlockGetPtr(m);
  172.     if (not ptr) return 6;
  173.     if (ptr[0] != '0123' or ptr[1] != '4567') return 7;
  174.     REALUnlockObject((REALobject)m);
  175.     
  176.     return 0;
  177. }
  178.  
  179. static REALpicture TestNewPicture(REALcontrolInstance)
  180. {
  181.     // Test picture creation and manipulation.
  182.     REALpicture p = REALNewPicture(64,64,32);
  183.     if (not p) return p;
  184.     
  185.     REALgraphics g = REALGetPictureGraphics(p);
  186.     if (g) {
  187.         #if WIN32
  188.             // use REALGraphicsDC followed by Win32 drawing commands...
  189.         #else
  190.             REALSelectGraphics(g);
  191.             RGBColor red = {0xFFFF,0,0}, green = {0,0xFFFF,0};
  192.             RGBForeColor(&red);
  193.             Rect r = {0,0,32,32};
  194.             PaintRect(&r);
  195.             OffsetRect(&r, 32,32);
  196.             RGBForeColor(&green);
  197.             PaintRect(&r);
  198.         #endif
  199.     }
  200.     
  201.     return p;
  202. }
  203.  
  204. static int TestStringToOSType(REALcontrolInstance, REALstring s)
  205. {
  206.     return REALstringToOSType(s);
  207. }
  208.  
  209. static int testColorComponent(REALcontrolInstance instance, int bit)
  210. {
  211.     ControlData(testControl, instance, testData, data);
  212.  
  213.     if (bit == 0)
  214.         return data->fillColor >> 16;
  215.     else if (bit == 1)
  216.         return (data->fillColor >> 8) & 0xff;
  217.     else
  218.         return (data->fillColor) & 0xff;
  219. }
  220.  
  221. REALproperty testProperties[] = {
  222.     { "Appearance", "BackColor", "Color", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(testData, fillColor) }
  223. };
  224.  
  225. REALmethodDefinition testMethods[] = {
  226.     { (REALproc) testMakeRed, REALnoImplementation, "MakeRed" },
  227.     { (REALproc) testColorComponent, REALnoImplementation, "ColorComponent(v as integer) as integer" },
  228.     { (REALproc) TestNewMemoryBlock, REALnoImplementation, "NewMemoryBlock(size as Integer) as MemoryBlock" },
  229.     { (REALproc) TestMemoryBlock, REALnoImplementation, "TestMemoryBlock as Integer" },
  230.     { (REALproc) TestNewPicture, REALnoImplementation, "TestNewPicture as Picture" },
  231.     { (REALproc) TestStringToOSType, REALnoImplementation, "TestStringToOSType(s as String) as Integer" },
  232. };
  233.  
  234. REALcontrolBehaviour testBehaviour = {
  235.     testInit,
  236.     nil,
  237.     testDraw,
  238.     nil,
  239.     nil,
  240.     nil
  241. };
  242.  
  243. static void globTest(int *foo)
  244. {
  245.     for (int i=0; i<*foo; i++) {
  246.         #if !WIN32
  247.             SysBeep(3);
  248.         #endif
  249.     }
  250.     *foo = 42;
  251. }
  252.  
  253. REALcontrol testControl = {
  254.     kCurrentREALControlVersion,
  255.     "PluginTest",
  256.     sizeof(testData),
  257.     REALcontrolAcceptFocus | REALcontrolFocusRing,
  258.     128,
  259.     129,
  260.     32, 32,
  261.     testProperties,
  262.     sizeof(testProperties) / sizeof(REALproperty),
  263.     testMethods,
  264.     sizeof(testMethods) / sizeof(REALmethodDefinition),
  265.     testEvents,
  266.     sizeof(testEvents) / sizeof(REALevent),
  267.     &testBehaviour
  268. };
  269.  
  270. static int testGetControlHandle(REALcontrolInstance instance)
  271. {
  272.     return (int)REALGetControlHandle(instance);
  273. }
  274.  
  275. REALmethodDefinition testGetControlHandleDef =
  276.     { (REALproc) testGetControlHandle, REALnoImplementation, "TestGetControlHandle(c as Control) as Integer" };
  277.  
  278.  
  279. void PluginEntry(void)
  280. {
  281.     #if CARBON
  282.         DebugStr("\pRegistering control");
  283.     #endif
  284.     REALRegisterControl(&testControl);
  285.  
  286.     REALmethodDefinition globTestMethod = { (REALproc) globTest, REALnoImplementation, 
  287.         "PluginByRefTest(ByRef foo as Integer)" };
  288.     REALRegisterMethod(&globTestMethod);
  289.     REALRegisterMethod(&testGetControlHandleDef);
  290. }
  291.